<?php
namespace BP\En;
class EntityOID extends Entity {
	public function getIsInnKey () 
	{
		return  FALSE ;
	}
	public function getPK () 
	{
		return "OID";
	}
	public function getOID () 
	{
		try 
		{
			return $this->GetValIntByKey(EntityOIDAttr::$OID);
		}
		catch (\Exception $e)
		{
			return 0;
		}
	}
	public function setOID ($value) // [long value]
	{
		$this->SetValByKey(EntityOIDAttr::$OID, $value);
	}
	public static function constructor__ () 
	{
		$me = new self();
		return $me;
	}
	public static function constructor__I ($oid) // [int oid]
	{
		$me = new self();
		$me->SetValByKey(EntityOIDAttr::$OID, $oid);
		$me->Retrieve();
		return $me;
	}
	public function DirectInsert () 
	{
		$this->setOID(DBAccess->GenerOID());
		return parent->DirectInsert();
	}
	public function InsertAsNew () 
	{
		$this->setOID(0);
		$this->Insert();
	}
	public function getIsExits () 
	{
		if (($this->getOID() == 0))
		{
			return  FALSE ;
		}
		try 
		{
			$selectSQL = (((((("SELECT " . $this->getPKField()) . " FROM ") . $this->getEnMap()->getPhysicsTable()) . " WHERE OID=") . $this->getHisDBVarStr()) . "v");
			$ens = new Paras();
			$ens->Add("v", $this->getOID());
			switch ($this->getEnMap()->getEnDBUrl()->getDBUrlType()) {
				case $AppCenterDSN:
					return DBAccess->IsExits($selectSQL, $ens);
				default:
					throw new \RuntimeException(("没有设计到�??" . $this->getEnMap()->getEnDBUrl()->getDBType()));
			}
		}
		catch (\RuntimeException $ex)
		{
			$this->CheckPhysicsTable();
			throw $ex;
		}
	}
	protected function beforeDelete () 
	{
		if ((parent->beforeDelete() ==  FALSE ))
		{
			return  FALSE ;
		}
		try 
		{
			if (($this->getOID() < 0))
			{
				throw new \RuntimeException((("@实体[" . $this->getEnDesc()) . "]没有被实例化，不能Delete()."));
			}
			return  TRUE ;
		}
		catch (\RuntimeException $ex)
		{
			throw new \RuntimeException(((("@[" . $this->getEnDesc()) . "].beforeDelete err:") . $ex->getMessage()));
		}
	}
	protected function beforeUpdateInsertAction () 
	{
		return parent->beforeUpdateInsertAction();
	}
	protected function beforeInsert () 
	{
		if (($this->getOID() == -999))
		{
			return parent->beforeInsert();
		}
		if (($this->getOID() > 0))
		{
			throw new \RuntimeException((((("@[" . $this->getEnDesc()) . "], 实体已经被实例化 oid=[") . $this->getOID()) . "]，不能Insert."));
		}
		if ($this->getIsInnKey())
		{
			$this->setOID(-1);
		}
		else
		{
			$this->setOID(DBAccess->GenerOID());
		}
		return parent->beforeInsert();
	}
	protected function beforeUpdate () 
	{
		if ((parent->beforeUpdate() ==  FALSE ))
		{
			return  FALSE ;
		}
		return  TRUE ;
	}
	protected function getSerialKey () 
	{
		return "OID";
	}
	public function SaveAsNew () 
	{
		try 
		{
			$this->setOID(DBAccess->GenerOIDByKey32($this->getSerialKey()));
			$this->RunSQL(SqlBuilder::Insert($this));
		}
		catch (\RuntimeException $ex)
		{
			$this->CheckPhysicsTable();
			throw $ex;
		}
	}
	public function InsertAsOID_I ($oid) // [int oid]
	{
		$this->SetValByKey("OID", $oid);
		try 
		{
			$this->RunSQL(SqlBuilder::Insert($this));
		}
		catch (\RuntimeException $ex)
		{
			$this->CheckPhysicsTable();
			throw $ex;
		}
	}
	public function InsertAsOID_L ($oid) // [long oid]
	{
		try 
		{
			$this->SetValByKey("OID", -999);
			$this->beforeInsert();
			$this->SetValByKey("OID", $oid);
			$this->RunSQL(SqlBuilder::Insert($this));
			$this->afterInsert();
		}
		catch (\RuntimeException $ex)
		{
			$this->CheckPhysicsTable();
			throw $ex;
		}
	}
	public function SaveAsOID ($oid) // [long oid]
	{
		$this->SetValByKey("OID", $oid);
		if (($this->getIsExits() ==  FALSE ))
		{
			$this->InsertAsOID_L($oid);
		}
		$this->Update();
	}
}
?>
